<?php
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends BaseController {

	public function actionIndex() {
		return $this->render('index');
	}

	public function actionLogin() {
		if (!\Yii::$app->user->isGuest) {
			return $this->goHome();
		}
		
		$model = new LoginForm();
		if ($model->load(Yii::$app->request->post()) && $model->login()) {
			return $this->goBack();
		}
		return $this->render('login', [
			'model'=>$model
		]);
	}

	public function actionLogout() {
		Yii::$app->user->logout();
		
		return $this->goHome();
	}

	public function actionContact() {
		$model = new ContactForm();
		if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
			Yii::$app->session->setFlash('contactFormSubmitted');
			
			return $this->refresh();
		}
		return $this->render('contact', [
			'model'=>$model
		]);
	}

	public function actionAbout() {
		return $this->render('about');
	}

	public function actionError() {
		$exception = Yii::$app->errorHandler->exception;
		if ($exception !== null) {
			if ($this->isAjax()) {
				return [
					'result'=>'failure',
					'code'=>$exception->getCode(),
					'file'=>defined('YII_DEBUG') ? $exception->getFile() : '',
					'line'=>defined('YII_DEBUG') ? $exception->getLine() : '',
					'trace'=>defined('YII_DEBUG') ? $exception->getTraceAsString() : '',
					'message'=>$exception->getMessage()
				];
			} else {
				return $this->render('error', [
					'message'=>$exception->getMessage()
				]);
			}
		}
	}
}
